/**
 * 
 */
package com.p2p.base.service.impl;

import java.io.File;
import java.util.Map;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import com.p2p.base.model.MailTemplate;
import com.p2p.base.service.EmailService;

import freemarker.template.Template;

/**
 * @author ruiqi
 *
 */
//旧的代码，copy备用
@Deprecated
public class EmailServiceImpl2 implements EmailService {

	@Resource
	private JavaMailSender mailSender;
	// private FreeMarkerConfigurer freeMarkerConfigurer;
	private static final String ENCODING = "utf-8";

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	/*
	 * public void setFreeMarkerConfigurer(FreeMarkerConfigurer
	 * freeMarkerConfigurer) { this.freeMarkerConfigurer = freeMarkerConfigurer;
	 * }
	 */

	/* *//**
	 * 发送带附件的html格式邮件
	 */
	/*
	 * public void sendEmail(String title,String text,Map<String,File>
	 * attachments,Map<String,File> imgs) { MimeMessage msg = null; try { msg =
	 * mailSender.createMimeMessage(); MimeMessageHelper helper = new
	 * MimeMessageHelper(msg, true, ENCODING);
	 * helper.setFrom("service@estore.com");
	 * helper.setTo("zhangsan@estore.com");
	 * helper.setSubject(MimeUtility.encodeText(title, ENCODING, null));
	 * helper.setText(text, true); // true表示text的内容为html
	 * 
	 * 
	 * for(Map.Entry<String, File> entry:imgs.entrySet()){ //
	 * 添加内嵌文件，第1个参数为cid标识这个文件,第2个参数为资源 helper.addInline(entry.getKey(),
	 * entry.getValue()); // 附件内容 }
	 * 
	 * for(Map.Entry<String, File> entry:attachments.entrySet()){ //
	 * 这里的方法调用和插入图片是不同的，解决附件名称的中文问题
	 * helper.addAttachment(MimeUtility.encodeWord(entry.getKey()),
	 * entry.getValue()); // 附件内容 }
	 * 
	 * } catch (Exception e) { throw new RuntimeException("error happens", e); }
	 * mailSender.send(msg); System.out.println("邮件发送成功..."); }
	 */
	public void sendEmail(MailTemplate mailTemplate, String toEmail,
			Map<String, Object> model,Map<String,File> attachMap) {
		MimeMessage msg = null;
		try {
			msg = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(msg, true,
					ENCODING);
			helper.setFrom(mailTemplate.getFrom());
			helper.setTo(toEmail);
			helper.setSubject(MimeUtility.encodeText(mailTemplate.getSubject(),
					ENCODING, null));
			helper.setText(getMailText(mailTemplate.getFtlTemplate(), model),
					true); // true表示text的内容为html

			//固有文件
			for (Map.Entry<String, File> entry : mailTemplate.getImgs()
					.entrySet()) {
				// 添加内嵌文件，第1个参数为cid标识这个文件,第2个参数为资源
				helper.addInline(entry.getKey(), entry.getValue()); // 附件内容
			}
			//固有附件
			for (Map.Entry<String, File> entry : mailTemplate.getAttachments()
					.entrySet()) {
				// 这里的方法调用和插入图片是不同的，解决附件名称的中文问题
				helper.addAttachment(MimeUtility.encodeWord(entry.getKey()),
						entry.getValue()); // 附件内容
			}
			//额外附件
			for (Map.Entry<
					String, File> entry : attachMap.entrySet()){
				helper.addAttachment(MimeUtility.encodeWord(entry.getKey()),
						entry.getValue()); // 附件内容
			}

		} catch (Exception e) {
			throw new RuntimeException("error happens", e);
		}
		//暂时注释
		//mailSender.send(msg);
		System.out.println("邮件发送成功...");
	}

	/**
	 * 通过模板构造邮件内容
	 */
	public String getMailText(Template template, Map<String, Object> model)
			throws Exception {
		// 通过指定模板名获取FreeMarker模板实例
		// Template template =
		// freeMarkerConfigurer.getConfiguration().getTemplate("registe.html");

		// 解析模板并替换动态数据，最终content将替换模板文件中的${content}标签。
		String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(
				template, model);
		System.out.println(htmlText);
		return htmlText;
	}
}
